#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define BASE 400000
#define MAX 800000

using namespace std;
typedef pair<int, int> Cow;
vector<Cow> cows;
int N;
int dp[MAX + 5];


void solve();
int main() {
    cin >> N;
    int p, q;
    while (N--) {
        cin >> p >> q;
        if (p < 0 && q < 0) continue;
        cows.push_back(make_pair(p, q));
    }
    solve();
    return 0;
}

void solve() {
    fill(dp, dp + MAX + 5, -(1 << 30));
    dp[BASE] = 0;
    // dp[j] = max(dp[j], dp[j - p]
    for (int i = 0; i < cows.size(); i++) {
        int p = cows[i].first, q = cows[i].second;
        if (p >= 0) {
            for (int j = MAX; j >= p; j--)
                dp[j] = max(dp[j], dp[j - p] + q);
        } else {
            for (int j = 0; j <= MAX + p; j++)
                dp[j] = max(dp[j], dp[j - p] + q);
        }
        
    }


    int res = 0;
    for (int i = BASE; i <= MAX; i++) {
        if (dp[i] >= 0) res = max(res, dp[i] + i - BASE);
    }
    cout << res << endl;

}